package com.example.yichuangyun.mvvm.ui.fragment;

import android.os.Bundle;
import android.view.View;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentContactsDataDetailsBinding;
import com.example.yichuangyun.dialog.MyDialog;
import com.example.yichuangyun.model.contacts.ContactWayModel;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.ui.activity.view.ContactsDetailsViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.ContactWayAdapter;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.RequestUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 联系人详情-资料详情
 */
public class ContactsDataDetailsFragment extends BaseFragment<FragmentContactsDataDetailsBinding, ContactsDetailsViewModel> {

    private String contactId;
    private int contactType;
    private String mTelNumber;
    private String clientName;
    private List<ContactWayModel> contactWayData = new ArrayList<>();
    private ContactWayAdapter adapter;
    private MyDialog myDialog;
    private boolean isOpen;

    @Override
    protected ContactsDetailsViewModel bindModel() {
        return getViewModel(this, ContactsDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {
        initDialog();
        adapter = new ContactWayAdapter(mContext, contactWayData);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void lazyLoadData() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            contactType = bundle.getInt(Constant.contactTypeKey);
            contactId = bundle.getString(Constant.idKey);
            viewModel.getContactsDetails(contactId, contactType);
        }
    }

    @Override
    protected void initClick() {
        adapter.setOnCallListener(number -> {
            if (CommonUtil.isCanClick()) {
                mTelNumber = number;
                RequestUtils.checkCallNum(mContext, mTelNumber, myDialog, 0, true);
            }
        });

        viewModel.detailsModel.observe(this, model -> {
            clientName = model.getUsername();
            binding.tvContactsName.setText(CommonUtil.setText(clientName));
            binding.tvSex.setText(model.getSex() == 1 ? "男" : model.getSex() == 2 ? "女" : "未知");
            contactWayData = model.getTels();
            adapter.onRefresh(contactWayData);

            binding.tvContactsFrom.setText(CommonUtil.setText(model.getContactSource()));
            binding.tvFollowStatus.setText(CommonUtil.setText(model.getFollowStatusName()));
            binding.tvContactsWeight.setText(String.valueOf(model.getContactWeight()));
            binding.tvRemark.setText(CommonUtil.setText(model.getRemark()));
            binding.tvOpen.post(() -> binding.tvOpen.setVisibility(binding.tvRemark.getLineCount() > 5 ? View.VISIBLE : View.GONE));
            binding.tvContactsId.setText(CommonUtil.setText(model.getContactNo()));
            binding.tvCreatePerson.setText(CommonUtil.setText(model.getFoundUser()));
            binding.tvCreatePersonDept.setText(CommonUtil.setText(model.getFoundUserDept()));
            binding.tvCreateTime.setText(CommonUtil.setText(model.getCreateTime()));
            binding.tvUpdateTime.setText(CommonUtil.setText(model.getUpdateTime()));
            binding.tvLastFollowTime.setText(CommonUtil.setText(model.getLastFollowTime()));
            binding.tvNextFollowTime.setText(CommonUtil.setYHMTime(model.getNextFollowTime()));

            List<String> tags = model.getLabelList();
            binding.tagCloudView.setTags(tags);
        });

        binding.tvOpen.setOnClickListener(v -> {
            isOpen = !isOpen;
            if (isOpen) {
                binding.tvOpen.setText(getString(R.string.no_expand));
                binding.tvRemark.setMaxLines(Integer.MAX_VALUE);
            } else {
                binding.tvOpen.setText(getString(R.string.expand));
                binding.tvRemark.setMaxLines(5);
            }
        });
    }

    private void initDialog() {
        myDialog = new MyDialog(getActivity()) {
            @Override
            public void leftClick() {
                RequestUtils.callNum(mContext, mTelNumber);
                Constant.pushBusinessId = "1";
                RequestUtils.addContactsFollow(contactId, clientName, mTelNumber);
            }

            @Override
            public void rightClick() {
                RequestUtils.callbackOutbound2(mContext, mTelNumber, contactId, clientName);
            }
        };
    }

    public static ContactsDataDetailsFragment newInstance(String contactId, int contactType) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.contactTypeKey, contactType);
        bundle.putString(Constant.idKey, contactId);
        ContactsDataDetailsFragment detailsDataFragment = new ContactsDataDetailsFragment();
        detailsDataFragment.setArguments(bundle);
        return detailsDataFragment;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusModel eventBusModel) {
        if (null != eventBusModel) {
            if (eventBusModel.getMsg().equals(Constant.refreshContactDetailsData)) {
                viewModel.getContactsDetails(contactId, contactType);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
